/*
 * AutismSiteMasterController.java
 *
 * Created on October 24, 2006, 3:59 PM
 */

package com.asaga.controller;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

import java.util.*;

import com.asaga.tools.*;

/**
 *
 * @author jeremys
 * @version
 */
public class AutismSiteMasterController extends HttpServlet {
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        HttpSession session = request.getSession();
        String action = request.getParameter("action");
        String submit = request.getParameter("submit");
        String message = null;
        AutismValidationHandler avh = new AutismValidationHandler(request, session);
        JeremyDBConnectObj jdbcon = new JeremyDBConnectObj(); // Jeremy's test db connection object.
        
        
        
        /* Original Author: Jeremy
         * Last Modified by: Jeremy, 2006-12-03
         * Comments: This block handles session and user authentication.  The
         *              user is either authenticated to the portal, registered,
         *              or session is verified for any other action.        
         */
         if(action==null){
            response.sendRedirect("login.jsp");
         }
         else if (action.equals("register")){
            RegistrationHandler rh = new RegistrationHandler(request);
            if(rh.RegisterPerson()==false){
                message = rh.getErrorMessage();
                SystemClient_DBImpl scdb = rh.getRegistrationInfo();
                session.setAttribute("errorMessage", message);
                session.setAttribute("clientInfo",scdb);
                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/register.jsp");         
                dispatcher.forward(request,response);
            }
            else{
                message = rh.getErrorMessage();
                session.setAttribute("errorMessage", message);
                session.setAttribute("registration_handler",rh);           
                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/confirmation.jsp");         
                dispatcher.forward(request,response);
            }
        }
        else if(action.equals("login")){
            boolean sessionflag = false;           
            sessionflag = avh.makeUserSession(request,session);
            if(sessionflag==false){
                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/login.jsp?message=Incorrect Username Or Password");
                dispatcher.forward(request,response);
            }else{
                session.setAttribute("username",request.getParameter("username"));
                session.setAttribute("password",request.getParameter("password"));
                //session.setAttribute("dbmessage",jdbcon.toString()); --for testing db connection only
                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/portal.jsp");
                dispatcher.forward(request,response);                
            }
        }else{  // If user is not logging in or registering, check session and do everything else.
            
            boolean sessionflag=false;
            sessionflag = avh.checkUserSession(session,response);
            if(sessionflag==false){
                //session.invalidate();
                response.sendRedirect("login.jsp");
            } 
                   
            /* Original Author: Jeremy
             * Last Modified by: Jeremy, 2006-12-03
             * Comments: This is the master controller/redirector     
             */
            else if(action.equals("logoff")){
                session.removeAttribute("username");
                response.sendRedirect("login.jsp");
            }
            else if(action.equals("addEventForm")){
                response.sendRedirect("addevent.jsp");
            }

            else if(action.equals("listEvents")){
                response.sendRedirect("myevents.jsp");
            }
            else if(action.equals("addevent")){
                CalendarHandler eh = new CalendarHandler(request);
                session.setAttribute("event", eh.getEvent());
                session.setAttribute("location", eh.getLocation());
                response.sendRedirect("verifyevent.jsp");
            }
            else if(action.equals("verifyevent")){
                CalendarHandler eh = new CalendarHandler();
                if(submit.equals("Yes")){
                    eh.setEvent((Event_DBImpl)session.getAttribute("event"));
                    eh.addEvent();
                    response.sendRedirect("portal.jsp");
                }
                else{
                    response.sendRedirect("addevent.jsp");
                }
            }
            else if(action.equals("myprofile")){
                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/myprofile.jsp");
                dispatcher.forward(request, response);
            }
            else if(action.equals("updateprofile")){
                SystemClient_DBImpl scdbup = (SystemClient_DBImpl)session.getAttribute("userProfile");
                UpdateHandler uh = new UpdateHandler(request,scdbup);
                String uname = (String)session.getAttribute("username");
                if(uh.UpdateInfo(uname)){
                    session.setAttribute("message",uh.getError());
                    session.setAttribute("userProfile","Profile successfully updated.");
                    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/myprofile.jsp");
                    dispatcher.forward(request, response);
                }
                else{
                    session.setAttribute("message",uh.getError());
                    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/jrs_error.jsp");
                    dispatcher.forward(request, response);                    
                }
            }
            else{            
                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
                dispatcher.forward(request, response);
            }
        }
        jdbcon.close();
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
